package com.da.mybatis.util;

import java.lang.reflect.Method;

import com.da.mybatis.interceptor.UserPermissionAop;
import org.apache.ibatis.mapping.MappedStatement;

public class PermissionUtils {

    public static UserPermissionAop getPermissionByDelegate(MappedStatement mappedStatement) {
        UserPermissionAop permissionAop = null;
        try {
            String id = mappedStatement.getId();
            String className = id.substring(0, id.lastIndexOf("."));
            String methodName = id.substring(id.lastIndexOf(".") + 1, id.length());
            final Class cls = Class.forName(className);
            final Method[] method = cls.getMethods();
            for (Method me : method) {
                if (me.getName().equals(methodName) && me.isAnnotationPresent(UserPermissionAop.class)) {
                    permissionAop = me.getAnnotation(UserPermissionAop.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return permissionAop;
    }
}